package com.alogic.textfilter.impl;

import com.alogic.jsoup.DefaultWhiteList;
import com.alogic.jsoup.HtmlCleaner;
import com.alogic.jsoup.WhiteList;
import com.alogic.textfilter.TextFilter;
import com.anysoft.util.*;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.w3c.dom.Element;

/**
 * html清洗
 *
 * @since 1.6.13.10 [20200827 duanyy]
 *
 * @version 1.6.13.13 [20200904 duanyy] <br>
 * - 采用新的HtmlCleaner <br>
 * @version 1.6.13.22 [20201026 duanyy] <br>
 * - 增加缓存filter结果的功能
 */
public class HtmlClean extends TextFilter.Abstract {
    protected WhiteList whitelist = null;
    @Override
    public void onConfigure(Element root, Properties p) {
        Factory<WhiteList> f = new Factory<WhiteList>();
        try {
            whitelist = f.newInstance(root,p,"whitelist", DefaultWhiteList.class.getName());
        }catch (Exception ex){
            LOG.warn("Can not create whilte list:" + XmlTools.node2String(root));
            LOG.warn(ExceptionUtils.getStackTrace(ex));
        }
    }

    @Override
    public String onFilter(String text, Properties ctx) {
        return whitelist == null ? text: HtmlCleaner.clean(text,whitelist,ctx);
    }
}
